<?php
class greskeActions extends sfActions
{
   public function executeGreske(sfWebRequest $request)
  {
      isicsBreadcrumbs::getInstance()->addItem($this->getContext()->getI18N()->__('Greške'));
      $this->sent = 0;

      if($request->isMethod('post'))
      {
          $this->error = $this->greskeValidacija($request);
          if(!$this->error)
          {
              $greska = new Greska();
              $greska->setProfilId($this->getUser()->getAttribute('id'));
              $greska->setTip($request->getParameter('tip'));
              $greska->setNaslov($request->getParameter('naslov'));
              $greska->setText($request->getParameter('text'));
              if($greska->save())
              {
                $this->error = '<li class="saved">'.$this->getContext()->getI18N()->__('Vaša poruka je poslata!').'</li>';
                $this->sent = 1;
              }
          }
      }

      $this->greske = DbFinder::from('Greska')->findBy('ProfilId', $this->getUser()->getAttribute('id'));
  }

  private function greskeValidacija($request)
  {
      $err = '';
      if($request->getParameter('tip') == '')
      {
          $err .= '<li>'.$this->getContext()->getI18N()->__('Izaberite tip poruke!').'</li>';
      }

      if($request->getParameter('naslov') == '')
      {
          $err .= '<li>'.$this->getContext()->getI18N()->__('Upišite naslov poruke!').'</li>';
      }

      if($request->getParameter('text') == '')
      {
          $err .= '<li>'.$this->getContext()->getI18N()->__('Upišite tekst poruke!').'</li>';
      }

      if(strlen($err) > 0)
        return $err;
      return FALSE;
  }

  public function executePregledGreske(sfWebRequest $request)
  {
      $greska = DbFinder::from('Greska')->findPk($request->getParameter('greska_id'));
      isicsBreadcrumbs::getInstance()->addItem($this->getContext()->getI18N()->__('Greške'), 'user/greske');
      isicsBreadcrumbs::getInstance()->addItem($greska);

      $this->greska = DbFinder::from('Greska')->findPk($request->getParameter('greska_id'));

      if($greska->getStatus() == 2)
      {
        $greska->setStatus(3);
        $greska->save();
      }
  }

}